<?php
$objXajax->registerFunction("frmmenutabag_initPage");
$objXajax->registerFunction("frmmenutabag_loadGrid");
$objXajax->registerFunction("frmmenutabag_save");
$objXajax->registerFunction("frmmenutabag_edit");
$objXajax->registerFunction("frmmenutabag_loadPower");
  
function frmmenutabag_initPage(){
  global $objDbUpdate,$strCheck,$lang;
    $objResponse = new xajaxResponse();
    $strCheck = frmmenutabag_createTblHTMLPower(1);
    $objResponse->addAssign('frmmenutabag_Powers', 'innerHTML', $strCheck);
    $strMainMenu = frmmenutabag_createOptionFather();
    $objResponse->addAssign('frmmenutabag_tdMainmenu', 'innerHTML', $strMainMenu);  
/*    $arr = getArrService();
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/management/main_menutab_service_grid.html');
    $oTbs->MergeBlock('blk_data', $arr);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('frmmenutabag_serviceType', 'innerHTML', $oTbs->Source); */
  return    $objResponse->getXML();             
}
function frmmenutabag_save($params){
 global $objDbUpdate;
 $objResponse = new xajaxResponse();  
 //echo '<pre>'; print_r($params); die();
 $name_vi = addslashes($params['txtViMenu']);
 $name_en = addslashes($params['txtEnMenu']);
 $module = addslashes($params['txtModule']);
 $ordershow = $params['txtOrder'];
 //$menu_type =  $params['menuType'];
 //$service_type =  $params['cbbService']==''?0:$params['cbbService'];
 //$scope =  $params['scope'];
 $mainmenu =  $params['mainmenu'];
 $edit = $params['hdEdit'];
 $id = $params['hdId'];
 $arrPower =  $params['arrPower'];
 if($edit!=1){
    $sqlInsert = "INSERT INTO tbl_menu(name_vi,name_en,module,main_menu_id,active,date_created,date_update,ordershow) 
    VALUE('$name_vi','$name_en','$module','$mainmenu','0',now(),now(),'$ordershow')";     
   //echo $sqlInsert; die();
    $result    =  $objDbUpdate->Execute($sqlInsert);
    $id_insert = mysql_insert_id();
    $strValue = frmmenutabag_createValue($params,$id_insert);
    if(strlen($strValue)>0){
        $sqlInsert = "INSERT INTO tbl_menu_powertype(menu_id,power_id) VALUES$strValue";
        $objDbUpdate->Execute($sqlInsert);
    }        
 }else{
     $sql = "UPDATE tbl_menu SET name_vi ='$name_vi',name_en ='$name_en',module ='$module',ordershow ='$ordershow',
                main_menu_id='$mainmenu',date_update = now() WHERE id = '$id'";
    if($objDbUpdate->Execute($sql)){
        $sql = "DELETE FROM tbl_menu_powertype WHERE menu_id = '$id'";
        if($objDbUpdate->Execute($sql)){
             $strValue = frmmenutabag_createValue($params,$id);
         if(strlen($strValue)>0){
            $sqlInsert = "INSERT INTO tbl_menu_powertype(menu_id,power_id) VALUES$strValue"; 
            $objDbUpdate->Execute($sqlInsert);
         }  
        }
    }     
 }
 $objResponse->addScript("xajax_frmmenutabag_loadGrid('');"); 
 return   $objResponse->getXML($sqlInsert);
}

function frmmenutabag_createValue($params,$id_insert){
     $result = '';
     foreach($params as $key=>$val){
         if(substr($key,0,8)=='chkpower'&&$val==1){
             $val1 = substr($key,8);
            $result .= ",('$id_insert','$val1')"; 
         }
     }
     $result = strlen($result)>0?substr($result,1):'';
     return $result;
 }
 
function frmmenutabag_createOptionFather(){
global $objDbSelect,$lang;
  $sql = "SELECT id AS id, name_$lang as name from tbl_main_menu  ";
  $arrfather = $objDbSelect->GetArray($sql);
  $temp ='';
  $count = count($arrfather);
  for($i=0;$i<$count;$i++){
    $id = $arrfather[$i]['id'];
    $name = $arrfather[$i]['name'];
    $temp.= "<option value='$id'> $name </option>"  ;
  }
  $temp =  '<select id="frmmenutabag_mainmenu" class="cbbRequired"><option value="">----Chọn----</option>'.$temp.'</select>';
  return $temp;
}

function frmmenutabag_getOrderByFather($father_id){
  global $objDbSelect;
  $sql = "SELECT category_order AS cateorder from tbl_menu WHERE id = $father_id";
  $arrOrder = $objDbSelect->GetArray($sql);
  if(count($arrOrder)>0){
      return $arrOrder[0]['cateorder'] ;
  }
  return 1;
}

function frmmenutabag_createTblHTMLPower($numCol,$menu_id=''){
  global $objDbSelect;
  $lang = $_SESSION['lang'];
  $addCondition = '';
  if($menu_id!=''){
    $addCondition = " and id in (select power_type_id from tbl_mainmenu_powertype where main_menu_id = $menu_id)";    
  }  
  $sql =  " select id,name_$lang as name from tbl_power_type where flg_cus=0 $addCondition";
  $arrMenu = $objDbSelect->GetArray($sql);
      
  $count = count($arrMenu);
  $numadd = ($numCol- $count%$numCol)%$numCol;
  for($j=0;$j<$numadd;$j++){
      $arrPustemp= array();
      $arrPustemp[$count+$j-1] = array('id'=>0,'name'=>'');
      $arrMenu=array_merge($arrMenu,$arrPustemp);
  }
  $count = count($arrMenu);
  $tbl ="<table id='tblpower' style='padding-left: 20px' class='width100'>";
  for($i=0;$i<$count;$i++){
      $id = $arrMenu[$i]['id'];
      $name = $arrMenu[$i]['name'];
      $tdTemp='';
      if($id!=0){
       $tdTemp = "<td  style='padding-left: 10px'><input type='checkbox'  class='chkbox' onchange=\"fn_SubChkChange('frmmenutabag_chkAllPower','frmmenutabag_chkpower')\" checked='checked'  id='frmmenutabag_chkpower$id' value='$id' name='frmmenutabag_chkpower'  />$name</td>";   
      }else{
         $tdTemp ='<td></td>'; 
      }
      
      if($i%$numCol==0){
        $tbl .= "<tr>$tdTemp";  
      }else if($i%$numCol==$numCol-1){
        $tbl .= "$tdTemp</tr>";  
      }else{
         $tbl .= "$tdTemp"; 
      }
  }
  return $tbl.'</table>';
}

function frmmenutabag_loadGrid($main_menu_id=''){     
global $lang,$objDbSelect;
$lang ='vi';
$arrMenu = frmmenutabag_GetMenu($main_menu_id);
$objResponse = new xajaxResponse();
$oTbs = new clsTinyButStrong();
$oTbs->LoadTemplate('templates/'.$lang.'/management/main_menutab_ag_grid.html');
$oTbs->MergeBlock('blk_data', $arrMenu);
$oTbs->Show(TBS_NOTHING);
$objResponse->addAssign('frmmenutabag_divGrid', 'innerHTML', $oTbs->Source); 
$objResponse->addScriptCall("dataTableObj.CreatDataTableGroup('frmmenutabag_tbl');"); 
$objResponse->addScript('changeWaiting(1);');                              
return $objResponse->getXML();
}

function  frmmenutabag_GetMenu($main_menu_id=''){
global $objDbSelect,$lang;
$addCond = '';
if($main_menu_id!=''){
    $addCond = " AND main_menu_id = '$main_menu_id' ";
}
$sqlSelect = "  SELECT me.id,me.ordershow,me.name_vi,me.name_en,me.module,me.active,ma.name_$lang as main_menu FROM tbl_menu me join tbl_main_menu ma 
                on me.main_menu_id = ma.id WHERE 1 = 1 $addCond ";        
return $objDbSelect->GetArray($sqlSelect);
}

function  frmmenutabag_edit($id){
    global $objDbSelect;
    $objResponse = new xajaxResponse();
    $sql         = "select * from tbl_menu where id = '$id'";
    $arrMenu         = $objDbSelect->GetArray($sql);
    
     $sql         = "select * from tbl_menu_powertype where menu_id = '$id'";
     $arrPower         = $objDbSelect->GetArray($sql);            
    $name_vi = $arrMenu[0]['name_vi'];
    $name_en = $arrMenu[0]['name_en'];
    $main_menu_id = $arrMenu[0]['main_menu_id'];
    $module = $arrMenu[0]['module'];
   // $menu_type = $arrMenu[0]['menu_type'];
    $ordershow = $arrMenu[0]['ordershow'];
    
    $strId = '';
    for($i=0;$i<count($arrPower);$i++){
      $strId .= ','.$arrPower[$i]['power_id'] ; 
    }
    if(strlen($strId)>0){
        $strId= substr($strId,1);
    }
    
    $strCheck = frmmenutabag_createTblHTMLPower(1,$main_menu_id);
    $objResponse->addAssign('frmmenutabag_Powers', 'innerHTML', $strCheck);
  
    $objResponse->addScript("$('#frmmenutabag_txtViMenu').val('$name_vi');");
    $objResponse->addScript("$('#frmmenutabag_txtEnMenu').val('$name_en');");
    $objResponse->addScript("$('#frmmenutabag_mainmenu').val('$main_menu_id');");
    $objResponse->addScript("$('#frmmenutabag_txtModule').val('$module');");
    $objResponse->addScript("$('#frmmenutabag_txtOrder').val('$ordershow');");
    //$objResponse->addScript("$('#frmmenutabag_cbbService').val($service_type);");
   // $objResponse->addScript("$('#frmmenutabag_menuType$menu_type').attr('checked',true);");
   // $objResponse->addScript("$('#frmmenutabag_service$scope').attr('checked',true);");
    //$objResponse->addScript("frmmenutabag_showService($scope);");
    $objResponse->addScript("frmmenutabag_setPower('$strId');");
    $objResponse->addScript("fn_SubChkChange('frmmenutabag_chkAllPower','frmmenutabag_chkpower');");    
    $objResponse->addScript('changeWaiting(1);');
    
    return $objResponse->getXML();
} 

function frmmenutabag_loadPower($main_menu_id){
     global $objDbUpdate,$strCheck;
  $objResponse = new xajaxResponse();
  $strCheck = frmmenutabag_createTblHTMLPower(1,$main_menu_id);
  $objResponse->addAssign('frmmenutabag_Powers', 'innerHTML', $strCheck);
  return    $objResponse->getXML();             
}

//function getArrService(){
//    global $objDbUpdate,$lang;
//    $sql = "select id, name_$lang as name from tbl_service_type";
//    $arr = $objDbUpdate->GetArray($sql);
//    return $arr;
//}
  
  # end region process data
?>
